goog.provide('dkpoints.admin');

goog.require('dkpoints.auth');
goog.require('dkpoints.status');
goog.require('dkpoints.admin.upload');

// Module name. Used by the Place manager.
dkpoints.admin.name = "/admin";

/**
 * If the page is opened or refreshed on an admin page, the loadUI method
 * may be called before the auth.checkStatus() method fills in auth.authStatus.
 * These are used for backing off and retrying the check
 */
dkpoints.admin.authTimeoutDelay = 4;
dkpoints.admin.authAccumulatedDelay = 0;

// Array of setTimeout timeouts accumulated while checking auth status.
dkpoints.admin.authCheckTimeouts = [];

/**
 * Determine from the page URL what module to load.
 * @param {string} address The current address.
 * @return {Object} The package for the specific address.
 */
dkpoints.admin.getPackage = function(address) {
  var me = dkpoints.admin;
  
  var reg = /^\/admin\/(\w*)(\/.*\/?)?/;
  if(reg.test(address)) {
    var feature = reg.exec(address)[1];
    
    switch(feature) {
      case "upload":
        return dkpoints.admin.upload;
        break;
    }
  }
  
  return null;
};

/**
 * Main place handler first calls getPackage() to get the correct sub-package,
 * then calls loadUI with that package, so if authentication passes, load it up.
 * @param {Object} modPackage The package pulled with getPackage().
 * @param {string} address The address that was called.  Used by the subpackage to
 *                            load based on any parameters
 */
dkpoints.admin.loadUI = function(modPackage, address) {
  var me = dkpoints.admin;

  // This method will probably be called via setTimeout if an /admin/* page
  // is loaded directly via bookmark or refresh.  Save the address passed in
  // by main.loadPlace().
  if(address) {
    me.authCheckTimeouts = [];
    me.token = address;
    me.packageToLoad = modPackage;
  }
  
  var authLevel = -1;
  try {
    // May throw an exception if there is a pending auth check.
    authLevel = dkpoints.auth.checkAuthLevel();
    if(authLevel != -1) {
      while(me.authCheckTimeouts.length > 0) {
        window.clearTimeout( me.authCheckTimeouts.pop() );
      }
    }
    
    if(authLevel == 0) {
      dkpoints.errors.notAuthorized(false);
    }
    else if(authLevel == 1) {
      dkpoints.errors.notAuthorized(true);
    }
    else if(authLevel == -1) {
      dkpoints.errors.genericError("Error validating authentication");
    }
    else {
      // Parse the URL and go to that place.
      me.packageToLoad.loadUI(address || me.token);
    }
  }
  catch (err) {
    if(err.message == dkpoints.auth.AUTHCHECKPENDING) {
      if(me.authAccumulatedDelay < dkpoints.auth.timeoutDelay) {
      
        // Back off a little more every time.
        me.authTimeoutDelay *= 2;
        me.authAccumulatedDelay += me.authTimeoutDelay;
        
        me.authCheckTimeouts.push(setTimeout(dkpoints.admin.loadUI, me.authTimeoutDelay));
      }
      else {
        dkpoints.errors.genericError("Unable to verify authentication");
      }
    }
    else {
      throw err;
    }
  }
};